function addAddressToMap(response) {
    if (!response || response.Status.code != 200)
    {
        $("#locationpref_status").html("<span style=\"color:red\">Sorry, we were unable to geocode that address!</span>");
    }
    else
    {
        $("#locationpref_status").html("Address looks fine!");
    }
}
function text_locationpref_init()
{
    var textField = $("#locationpref");
    var textValue = textField.val();
    textField.focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == textValue
        }).val("");
    });
    textField.blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).val(textValue);
        if($(this).val()!="" &&  $(this).val() != textValue)
        {
            $("#locationpref_status").html("<img src=\"./images/icon_wait.gif\"/>");
            geocoder.getLocations(textField.val(), addAddressToMap);
        }

    });
}
function text_twitteruserid_init()
{
    var textField = $("#twitteruserid");
    var textValue = textField.val();
    textField.focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == textValue
        }).val("");
    });
    textField.blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).val(textValue);
        if($(this).val()!="" &&  $(this).val() != textValue)
        {
            $("#twitteruserid_status").html("<img src=\"./images/icon_wait.gif\"/>");
            $.getJSON("http://twitter.com/users/show/"+textField.val()+".json?callback=?",
                function(data)
                {
                    if(data.name)
                    {
                        $("#twitteruserid_status").html(data.name);
                    }
                    else
                    {
                        $("#twitteruserid_status").html("<span style=\"color:red\">Error!!!!</span>");
                    }
                });
        }
    });
}
function drawVisualization() {
    var data=new google.visualization.DataTable(user_list);
    visualization = new google.visualization.Table(document.getElementById('People'));
    visualization.draw(data, null);
}
function initialize() {
    if (GBrowserIsCompatible()) {


        /*Maps for direction*/
        map = new GMap2(document.getElementById("map_canvas"))
        gdir = new GDirections(map, document.getElementById("directions"));
        map.setCenter(defualt_location, 13);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
        /*setDirections("Hitech City, Hyderabad, Andhra Pradesh, India","Barista,Jubille Hills,Hyderabad");*/

        /*Map for Location Select*/
        /*map_location_select = new GMap2(document.getElementById("location_select_canvas"))
                    /*gdir = new GDirections(map, document.getElementById("directions"));*/
        /*map_location_select.setCenter(new GLatLng(17.420425,78.479805), 13);
                    map_location_select.addControl(new GSmallMapControl());
                    map_location_select.addControl(new GMapTypeControl());*/
        /*GEvent.addListener(map_location_select, "click", getAddress);*/
        geocoder = new GClientGeocoder();


        /*Tabs and other jquery stuf*/
        $("#tabs").tabs();
        $("#datepref").datepicker();
        text_twitteruserid_init();
        text_locationpref_init();
        drawVisualization();
        
    }
}
/*function getAddress(overlay, latlng) {
                if (latlng != null) {
                    address = latlng;
                    geocoder.getLocations(latlng, showAddress);
                }
            }

            function showAddress(response) {
                map_location_select.clearOverlays();
                if (!response || response.Status.code != 200) {
                    alert("Status Code:" + response.Status.code);
                } else {
                    place = response.Placemark[0];
                    point = new GLatLng(place.Point.coordinates[1],
                    place.Point.coordinates[0]);
                    marker = new GMarker(point);
                    map_location_select.addOverlay(marker);
                    marker.openInfoWindowHtml('<b>Address:</b>' + place.address + '<br>' );
                    $("#locationpref").val(place.address);
                }
            }*/
function setDirections(fromAddress,toAddress)
{
    gdir.load("from: " + fromAddress + " to: " + toAddress,
    {
        "locale": "en_US"
    });
}
function handleErrors(){
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_BAD_KEY)
        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
        alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
    else
        alert("An unknown error occurred.");
}
function onGDirectionsLoad(){
}
